package com.link.play.fit.ai.ui.weightcontrol.ac

import android.annotation.SuppressLint
import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.activity.addCallback
import androidx.lifecycle.ViewModelProvider
import com.link.play.fit.ai.R
import com.link.play.fit.ai.base.BaseActivity
import com.link.play.fit.ai.base.goToAc
import com.link.play.fit.ai.bean.UserGuide
import com.link.play.fit.ai.databinding.AcFastingTimeBinding
import com.link.play.fit.ai.kv.AuthCache.firstMeals
import com.link.play.fit.ai.kv.AuthCache.secondMeals
import com.link.play.fit.ai.kv.AuthCache.thirdMeals
import com.link.play.fit.ai.ui.dialog.SetTimeDialog
import java.text.SimpleDateFormat
import java.time.LocalTime
import java.util.Locale

class FastingTimeAc : BaseActivity<AcFastingTimeBinding>(AcFastingTimeBinding::inflate) {

    val viewModel by lazy(LazyThreadSafetyMode.NONE) {
        ViewModelProvider(this)[WeighControlViewModel::class.java]
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        viewModel.lastUserGuide = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            intent.getSerializableExtra("data", UserGuide::class.java) ?: UserGuide()
        } else {
            @Suppress("DEPRECATION")
            (intent.getSerializableExtra("data") as UserGuide?) ?: UserGuide()
        }


        onBackPressedDispatcher.addCallback {
            finish()
            overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right)
        }
        binding.flBack.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        binding.tvStartTime.setOnClickListener {

            SetTimeDialog(this).showDialog(viewModel.lastUserGuide.fastingStartTime.ifEmpty { "08:10:AM" }) {
                viewModel.lastUserGuide.fastingStartTime = it
                val startTime = convertTo24HourFormat(it)
                binding.tvStartTime.text = startTime
                val hour = when (viewModel.lastUserGuide.fastingGoal) {
                    0 -> 12
                    1 -> 10
                    2 -> 8
                    else -> 0
                }
                binding.tvEndTime.text = addHoursToTime(startTime, hour)
                binding.lls.visibility = View.GONE
                binding.tvNext.isEnabled = true
            }

        }


        binding.tvNext.setOnClickListener {
            goToAc(WeighControlReadyAc::class.java) {
                putExtra("data", viewModel.lastUserGuide)
            }
            overridePendingTransition(R.anim.slide_in_from_right, R.anim.slide_out_to_left)
        }
    }

    fun convertTo24HourFormat(time12h: String): String {
        // 定义输入的时间格式（12小时制）
        val time12hFormat = SimpleDateFormat("hh:mm:a", Locale.ENGLISH)
        // 定义输出的时间格式（24小时制）
        val time24hFormat = SimpleDateFormat("HH:mm", Locale.ENGLISH)

        // 将输入的时间字符串解析为Date对象
        val time = time12hFormat.parse(time12h)
        // 将Date对象格式化为24小时制的时间字符串
        return time24hFormat.format(time)
    }

    @SuppressLint("DefaultLocale")
    fun addHoursToTime(time: String, hoursToAdd: Int): String {
        // 将输入的时间字符串解析为 LocalTime 对象
        val initialTime = LocalTime.parse(time)
        // 将指定小时数加到时间上
        val resultTime = initialTime.plusHours(hoursToAdd.toLong())
        // 返回格式化后的时间字符串（HH:mm 格式）
        return String.format("%02d:%02d", resultTime.hour, resultTime.minute)
    }
}